/* Copyright (c) [2025] [zhi1208@126.com]
 * SPDX-License-Identifier: MIT
 */

/*********************
 *      INCLUDES
 *********************/
#include "lvgl.h"
#include "hmi_pg_def.h"
#include "hmi_config.h"
#include "hmi_top_bar.h"
#include "hmi_anim.h"
#include "hmi_bg.h"
#include "hmi_list_item.h"
#include "hmi_png.h"
#include "hmi_pages.h"
#include "hmi_pg_navigation.h"

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/
static void create();
static void del();
static void enter_anim(lv_anim_completed_cb_t cb, uint32_t delay);
static uint32_t exit_anim(lv_anim_completed_cb_t cb, hmi_pg_t *enter_pg);

static void dt_item_click_cb(lv_event_t *e);
static void wifi_item_click_cb(lv_event_t *e);

/**********************
 *  STATIC VARIABLES
 **********************/
static hmi_pg_t setting = {
    .name = "设置",
    .del_on_pop = 1,
    .bg_color = 0x3ec1d3,
    .create = create,
    .del = del,
    .enter_anim = enter_anim,
    .exit_anim = exit_anim,
};

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/
hmi_pg_t *hmi_pg_setting()
{
    return &setting;
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
static void create()
{
    setting.bg_y = HMI_BG_NORMAL;

    setting.root = lv_obj_create(NULL);
    lv_obj_remove_style_all(setting.root);
    lv_obj_set_size(setting.root, lv_pct(100), lv_pct(100));
    lv_obj_set_style_pad_bottom(setting.root, 10, 0);
    lv_obj_set_flex_flow(setting.root, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_flex_align(setting.root, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);

    hmi_top_bar_create(setting.root, setting.name);

    lv_obj_t *cont = lv_obj_create(setting.root);
    lv_obj_set_flex_grow(cont, 1);
    lv_obj_set_width(cont, lv_pct(94));
    lv_obj_set_style_border_width(cont, 0, 0);
    lv_obj_set_flex_flow(cont, LV_FLEX_FLOW_COLUMN);

    hmi_list_item_cfg_t wifi_cfg = {
        .title = hmi_pg_wifi()->name,
        .icon = hmi_png("wifi"),
        .color = lv_color_hex(hmi_pg_wifi()->bg_color),
        .click_cb = wifi_item_click_cb};
    hmi_list_item_create(cont, &wifi_cfg);

    hmi_list_item_cfg_t dt_cfg = {
        .title = hmi_pg_dt()->name,
        .icon = hmi_png("calendar"),
        .color = lv_color_hex(hmi_pg_dt()->bg_color),
        .click_cb = dt_item_click_cb};
    hmi_list_item_create(cont, &dt_cfg);
}

static void del()
{
    lv_obj_delete_async(setting.root);
    setting.root = NULL;
}

static void enter_anim(lv_anim_completed_cb_t cb, uint32_t delay)
{
    hmi_enter_anim_default(setting.root, cb, delay);
}

static uint32_t exit_anim(lv_anim_completed_cb_t cb, hmi_pg_t *enter_pg)
{
    return hmi_exit_anim_default(setting.root, cb, enter_pg);
}

static void dt_item_click_cb(lv_event_t *e)
{
    LV_UNUSED(e);
    hmi_pg_push(hmi_pg_dt());
}

static void wifi_item_click_cb(lv_event_t *e)
{
    LV_UNUSED(e);
    hmi_pg_push(hmi_pg_wifi());
}